Skip to content

Instantly share code, notes, and snippets.

@BilalBudhani
Created January 31, 2013 10:04
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save BilalBudhani/4681837 to your computer and use it in GitHub Desktop.
Save BilalBudhani/4681837 to your computer and use it in GitHub Desktop.
PHP function to convert simple RSS to JSON
public function Parse ($url) {
$fileContents= file_get_contents($url);
$fileContents = str_replace(array("\n", "\r", "\t"), '', $fileContents);
$fileContents = trim(str_replace('"', "'", $fileContents));
$simpleXml = simplexml_load_string($fileContents);
$json = json_encode($simpleXml);
return $json;
}
@MitchellBouwman
Copy link

Great thank you!

@famaia
Copy link

famaia commented Nov 21, 2019

Thank you!
It works!

@vanduc1102
Copy link

what about the !CDATA

<description>
<![CDATA[
What does it mean to be a man in 2019? The Men Who Raised Us podcast speaks to inspirational men from all walks of life on how their journey through adolescence shaped who they are today. This podcast is presented in partnership with The Fathering Project. New episodes are released every Monday and Thursday - listen and subscribe to hear more.
]]>
</description>

I'm getting

    "description": {},

@vanduc1102
Copy link

vanduc1102 commented Nov 26, 2019

This one handles CDATA

function Parse($url){
     $simpleXml = simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA);
     $json = json_encode($simpleXml);
     return $json;
}

https://repl.it/@vanduc1102/Rss-To-JSON-sample

@menjilx
Copy link

menjilx commented Aug 7, 2021

This one handles CDATA

function Parse($url){
     $simpleXml = simplexml_load_file($url, "SimpleXMLElement", LIBXML_NOCDATA);
     $json = json_encode($simpleXml);
     return $json;
}

https://repl.it/@vanduc1102/Rss-To-JSON-sample

this one works! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment